﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.Constants;
using SPCFMedicineInformer.DocumentTypes;
using Vega.USiteBuilder;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using umbraco.NodeFactory;
using SPCFMedicineInformer.Helpers;
using umbraco;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_Abbreviations : System.Web.UI.MasterPage
    {
        public int PageNum { get; set; }
        public int TotalRec { get; set; }
        int limitPerPage = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageNum = 1;
                BindGrid();
                BindDropdown();
            }
        }

        void BindGrid()
        {
            IEnumerable<AbbreviationItem> list = NodeLocator.GetNodesOfExactType<AbbreviationItem>().Where(a => a.Abbreviation.ToLower().Contains(txtAbbreviation.Text.ToLower()) && 
                a.Meaning.ToLower().Contains(txtMeaning.Text.ToLower()));
            list = Sort(list);
            TotalRec = list.Count();
            list = list.Skip((PageNum - 1) * limitPerPage).Take(limitPerPage);
            DisplayPaging();
            rpt.DataSource = list;
            rpt.DataBind();
        }
        void Delete(int idToDelete)
        {
            if (idToDelete > 0)
            {
                Document doc = new Document(idToDelete);
                if (doc.Published)
                {
                    doc.UnPublish();
                    library.UnPublishSingleNode(doc.Id);
                }
                doc.delete();
                BindGrid();
            }
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                    case "paging":
                        PageNum = intTemp;
                        BindGrid();
                        break;
                }
            }
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            PageNum = 1;
            BindGrid();
        }
        protected void Repeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = (LinkButton)e.Item.FindControl("linkButPaging");
                Literal lit = (Literal)e.Item.FindControl("litPaging");
                if (PageNum.ToString() == lit.Text)
                {
                    lb.CssClass = "current";
                }
            }

        }
        void BindDropdown()
        {
            ddlTitles.Items.Add(new ListItem("Id", "Id"));
            ddlTitles.Items.Add(new ListItem("Abbreviation", "Abbreviation"));
            ddlTitles.Items.Add(new ListItem("Meaning", "Meaning"));
        }
        IEnumerable<AbbreviationItem> Sort(IEnumerable<AbbreviationItem> list)
        {


            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderBy(a => a.Id);
                        break;
                    case "Abbreviation":
                        list = list.OrderBy(a => a.Abbreviation);
                        break;
                    case "Meaning":
                        list = list.OrderBy(a => a.Meaning);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderByDescending(a => a.Id);
                        break;
                    case "Abbreviation":
                        list = list.OrderByDescending(a => a.Abbreviation);
                        break;
                    case "Meaning":
                        list = list.OrderByDescending(a => a.Meaning);
                        break;
                }
            }
            return list;

        }
        void DisplayPaging()
        {
            int maxPage = (TotalRec / limitPerPage) + (TotalRec % limitPerPage > 0 ? 1 : 0);
            List<string> pageList = new List<string>();
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString());
            }

            rptPaging.DataSource = pageList;
            rptPaging.DataBind();

        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/abbreviations/abbreviation-entry/");
        }
    }
}